import type { AxiosRequest } from './types'

import { checkStatus } from './checkStatus'
// import { message as messageNo, messageBox } from '@/utils/message'

export default (error: AxiosRequest.Error) => {
  const { response, code, message } = error || {}
  // 超时 和 网络异常提示
  const err: string = error?.toString?.() ?? ''
  const errorMessageMode = response?.config?.errorMessageMode || 'message'

  let errMessage = ''

  try {
    if (code === 'ECONNABORTED' && message.indexOf('timeout') !== -1) {
      errMessage = '接口请求超时,请刷新页面重试!'
    }
    if (err?.includes('Network Error')) {
      errMessage = '请检查您的网络连接是否正常!'
    }

    if (errMessage) {
      if (errorMessageMode === 'modal') {
        // messageBox.confirm(errMessage)
      } else if (errorMessageMode === 'message') {
        // messageNo.error(errMessage)
      }
      return Promise.reject(error)
    }
  } catch (error) {
    throw new Error(error as string)
  }

  // 其余https status 提示
  // @ts-ignore
  const msg: string = response?.data?.msg ?? ''
  const status = response && response.status

  status && checkStatus(status, msg)
  return Promise.reject(error)
}